
package cn.edu.heuet.bnve.activity.ScheduleActivity;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qmuiteam.qmui.util.QMUIDisplayHelper;

import cn.edu.heuet.bnve.view.CircleRelativeLayout;
import cn.edu.heuet.bnve.R;
import cn.edu.heuet.bnve.model.Schedule;
import cn.edu.heuet.bnve.database.ScheduleDao;
import cn.edu.heuet.bnve.databinding.ActivityNewHomeworkBinding;
import cn.edu.heuet.bnve.remind.SendAlarmBroadcast;
import cn.edu.heuet.bnve.utils.ColorUtil;
import cn.edu.heuet.bnve.utils.DialogUtil;
import cn.edu.heuet.bnve.utils.StringUtil;
import cn.edu.heuet.bnve.utils.XToastUtils;

public class NewHomeworkActivity extends AppCompatActivity {

    private ActivityNewHomeworkBinding binding;

    private AlertDialog setAlarmTimeDialog;

    TextView mscheduleDay;
    TextView mendTime;
    TextView mcolor;
    EditText mdecription;
    TextView mHomeworkTitle;

    private boolean isVibrate = false;

    private Schedule newSchedule = new Schedule();
    private ScheduleDao scheduleDao;

    private QMUIDisplayHelper ScreenUtils;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_with_view_pager);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_new_homework);

        initData();
        initView();
        initEvent();
    }


    public static void show(Context context) {
        context.startActivity(new Intent(context, NewHomeworkActivity.class));
    }

    public void initData(){

        newSchedule.setType(1);
    }

    public void initView(){
        mscheduleDay = binding.alarmDate;
        mendTime = binding.alarmEndTime;
        mcolor = binding.alarmColor;
        mdecription = binding.alarmDescription;
        mHomeworkTitle = binding.alarmTitle;
    }

    public void initEvent(){
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        binding.txSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                complete();
            }
        });
        binding.buttonCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        final DialogInterface.OnClickListener setAlarmTimeListener =
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        switch (which) {
                            case 0:
                                binding.alarmRemind.setText(R.string.text_alarm_time);
                                newSchedule.setAlarmTimeType(-1);
                                break;
                            case 1:
                                binding.alarmRemind.setText(R.string.text_alarm_time0);
                                newSchedule.setAlarmTimeType(0);
                                break;
                            case 2:
                                binding.alarmRemind.setText(R.string.text_alarm_time1);
                                newSchedule.setAlarmTimeType(1);
                                break;
                            case 3:
                                binding.alarmRemind.setText(R.string.text_alarm_time2);
                                newSchedule.setAlarmTimeType(2);
                                break;
                            case 4:
                                binding.alarmRemind.setText(R.string.text_alarm_time3);
                                newSchedule.setAlarmTimeType(3);
                                break;

                        }
                    }
                };
        binding.layoutAlarmRemind.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (setAlarmTimeDialog == null) {
                    setAlarmTimeDialog = new AlertDialog.Builder(NewHomeworkActivity.this)
                            .setTitle(R.string.set_alarm_time_items)
                            .setItems(R.array.set_alarm_time_items, setAlarmTimeListener)
                            .create();
                }
                setAlarmTimeDialog.show();
            }
        });
        binding.layoutAlarmLocal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { startActivityForResult(new Intent(NewHomeworkActivity.this, SetLocalActivity.class), 2); }
        });
        mscheduleDay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectDayDialog(mscheduleDay,NewHomeworkActivity.this,newSchedule);
            }
        });
        mendTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectTimeDialog(mendTime,1,NewHomeworkActivity.this,newSchedule);
            }
        });
        binding.layoutAlarmColor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) { DialogUtil.showSelectColorDialog(mcolor,NewHomeworkActivity.this,newSchedule);
            }
        });
        binding.swVibrate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Vibrator vibrator;
                vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
                if (!isVibrate) {
                    newSchedule.setIsVibrate(1);
                    vibrator.vibrate(500);
                    isVibrate = true;
                } else {
                    newSchedule.setIsVibrate(0);
                    vibrator.cancel();
                    isVibrate = false;
                }
            }
        });
        binding.buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                complete();
            }
        });
    }

    private void showHomeworkDialog(final Schedule newSchedule){
        final Dialog dialog = new SetDayDialog(this,R.style.dialog_normal);
        dialog.create();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_show_new_schedule);
        WindowManager.LayoutParams lp = window.getAttributes();
//        window.setWindowAnimations(R.style.activityAnimation);
        lp.gravity = Gravity.CENTER;
        lp.width = ScreenUtils.getScreenWidth(this)/6*5;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        final LinearLayout head = dialog.findViewById(R.id.ll_head);
        final TextView title = dialog.findViewById(R.id.tx_title);
        final TextView day = dialog.findViewById(R.id.tx_day);
        final TextView time = dialog.findViewById(R.id.tx_time);
        final TextView alarmType = dialog.findViewById(R.id.tx_alarmType);
        final TextView isVibrate = dialog.findViewById(R.id.tx_isVibrate);
        final TextView location = dialog.findViewById(R.id.tx_location);
        final TextView description = dialog.findViewById(R.id.tx_description);
        final CircleRelativeLayout circle  = dialog.findViewById(R.id.schedule_circle);

        circle.setColor(ColorUtil.getBackgroundColor(newSchedule.getAlarmColor()));
        title.setText(newSchedule.getTitle());
        day.setText(newSchedule.getYear()+"/"+newSchedule.getMonth()+"/"+newSchedule.getDay());
        time.setText("DDL: "+String.format("%02d",newSchedule.getEndTimeHour())+":"+String.format("%02d",newSchedule.getEndTimeMinute()));
        alarmType.setText(StringUtil.getAlarmText(newSchedule.getAlarmTimeType()));
        isVibrate.setText(newSchedule.getIsVibrate()==1? "Yes":"No");
        location.setText(newSchedule.getLocal());
        description.setText(newSchedule.getDescription());

        TextView okButton = dialog.findViewById(R.id.button_ok);
        okButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                scheduleDao = new ScheduleDao(NewHomeworkActivity.this);
                scheduleDao.insertAlarmDate(newSchedule);
                dialog.dismiss();
                SendAlarmBroadcast.startAlarmService(NewHomeworkActivity.this);
                XToastUtils.success(R.string.text_create_successful,1);
                finish();
            }
        });
        TextView cancelButton = dialog.findViewById(R.id.button_cancel);
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 2) {
            if (resultCode == 2) {
                if (data != null) {
                    String local = data.getStringExtra("local");
                    binding.alarmLocal.setText(local);
                    newSchedule.setLocal(local);
                }
            }
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void complete(){
        if (mHomeworkTitle.getText().length()!=0){
            newSchedule.setTitle(mHomeworkTitle.getText().toString());
        }
        else {
            DialogUtil.showSimpleTipDialog(NewHomeworkActivity.this,"请输入作业标题");
            return;
        }
        if(mscheduleDay.getText().equals(getResources().getString(R.string.select_day))){
            DialogUtil.showSimpleTipDialog(NewHomeworkActivity.this,"请选择活动日期");
            return;
        }
        else{
            newSchedule.setIsAllday(0);
            if(mendTime.getText().equals(getResources().getString(R.string.select_end_time))){
                DialogUtil.showSimpleTipDialog(NewHomeworkActivity.this,"请选择结束时间");
                return;
            }
        }
        if(mdecription.getText().length()!=0){
            newSchedule.setDescription(mdecription.getText().toString());
        }
        else {
            newSchedule.setDescription("无");
        }
        Log.i("wqs",newSchedule.toString());
        showHomeworkDialog((newSchedule));
    }

}